// Copyright (c) 2011, Grant China
// All rights reserved.

package org.gwtlcars.ui;

import java.util.Vector;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class LcarsPanelButton extends Composite implements HasClickHandlers {
    interface MyStyle extends CssResource {
        String normal();

        String vertical();

        String horizontal();

        String first_vertical();

        String first_horizontal();
    }

    private static LcarsPanelButtonUiBinder uiBinder = GWT
            .create(LcarsPanelButtonUiBinder.class);

    interface LcarsPanelButtonUiBinder extends
            UiBinder<Widget, LcarsPanelButton> {
    }

    @UiField
    Button button;

    @UiField
    MyStyle style;

    public enum State {
        UP, DOWN
    }

    private State current_state = State.UP;
    private ClickHandler click_handler = null;
    private String group_name = null;

    private String upColor = LcarsColor.LIGHT_ORANGE;
    private String downColor = "";

    public LcarsPanelButton(String firstName) {
        initWidget(uiBinder.createAndBindUi(this));
        button.setText(firstName);

        changeColor();
    }

    public void setVertical() {
        getElement().removeClassName(style.horizontal());
        getElement().removeClassName(style.first_vertical());
        getElement().addClassName(style.vertical());
    }

    public void setHorizontal() {
        getElement().removeClassName(style.vertical());
        getElement().addClassName(style.horizontal());
    }

    public void setFirstVertical() {
        getElement().removeClassName(style.vertical());
        getElement().addClassName(style.first_vertical());
    }

    public void setFirstHorizontal() {
        getElement().addClassName(style.first_horizontal());
    }

    private void changeColor() {
        if (current_state == State.UP) {
            if (downColor != "")
                button.removeStyleName(downColor);
            button.addStyleName(upColor);
        } else if (downColor != "") {
            button.removeStyleName(upColor);
            button.addStyleName(downColor);
        }
    }

    public void setColor(String color) {
        setUpColor(color);
    }

    public void setUpColor(String color) {
        button.removeStyleName(upColor);
        upColor = color;
        changeColor();
    }

    public void setDownColor(String color) {
        if (downColor != "")
            button.removeStyleName(downColor);
        downColor = color;
        changeColor();
    }

    @UiHandler("button")
    void onClick(ClickEvent e) {
        if ((this.group_name != null) && (current_state == State.DOWN))
            return;

        if (current_state == State.UP)
            current_state = State.DOWN;
        else
            current_state = State.UP;
        changeColor();

        if (click_handler != null)
            click_handler.onClick(e);

        if (this.group_name != null) {
            // change state for other buttons in group
            // first find the containing LcarsPanel
            Widget parent = this.getParent();
            while (parent.getClass().getSuperclass() != LcarsPanel.class)
                parent = parent.getParent();
            Vector<LcarsPanelButton> radio_buttons = ((LcarsPanel) parent)
                    .getRadioButtons();
            for (LcarsPanelButton btn : radio_buttons) {
                if (btn != this) {
                    btn.setState(State.UP);
                }
            }
        }
    }

    @Override
    public HandlerRegistration addClickHandler(ClickHandler handler) {
        click_handler = handler;
        return null;
    }

    public State getState() {
        return current_state;
    }

    public void setState(State s) {
        if (s == current_state)
            return;

        current_state = s;
        changeColor();

        if (current_state == State.DOWN)
            button.click();
    }

    public String getGroupName() {
        return group_name;
    }

    public void setGroupName(String group_name) {
        this.group_name = group_name;
    }
}
